import pandas as pd
from jinja2 import Environment, FileSystemLoader
# 读取CSV文件
df = pd.read_csv('D:/Courses/SmallThree/scrapy_spider/data.csv', encoding='gbk')


# 设置Jinja2环境
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('template.html')

# 为每个城市生成网页
for city, group in df.groupby('city'):
    # 渲染模板
    html_content = template.render(city=city, labs=group.to_dict(orient='records'))
    print(city)
    # 将渲染后的HTML保存到文件
    with open(f'D:/Courses/SmallThree/scrapy_spider/{city}_labs.html', 'w', encoding='utf-8') as f:
        f.write(html_content)

print("网页生成完成。")